package com.sxt.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sxt.sys.SYSConstast.SYSConstast;
import com.sxt.sys.domain.Role;
import com.sxt.sys.domain.User;
import com.sxt.sys.mapper.UserMapper;
import com.sxt.sys.service.UserService;
import com.sxt.sys.utils.DataGridView;
import com.sxt.sys.utils.Md5Utils;
import com.sxt.sys.vo.UserVo;
@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private UserMapper userMapper;
	
	
	@Override
	public User queryByUserLoginName(String loginname) {
		// TODO Auto-generated method stub
		return userMapper.queryByLoginName(loginname);
	}


	@Override
	public DataGridView queryAllUser(UserVo userVo) {
		Page<Object> page = PageHelper.startPage(userVo.getPage(),userVo.getLimit());
		List<User> data = userMapper.queryAllUser(userVo);
		return new DataGridView(page.getTotal(),data);
	}


	@Override
	public Integer addUser(UserVo userVo) {
		// TODO Auto-generated method stub
		userVo.setType(SYSConstast.PERMISSION_TYPR_TURE);
		userVo.setImgpath(SYSConstast.ADD_USER_IMAG);
		String salt = Md5Utils.createRandomStr();
		userVo.setSalt(salt);
		userVo.setPwd(Md5Utils.md5(SYSConstast.ADD_USER_PASSWORD, salt, 2));
		userVo.setOrdernum(userVo.getOrdernum()+1);
		return userMapper.insertSelective(userVo);
	}


	@Override
	public Integer deleteUser(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.deleteByPrimaryKey(id);
	}


	@Override
	public Integer updateUser(UserVo userVo) {
		// TODO Auto-generated method stub
		return userMapper.updateByPrimaryKeySelective(userVo);
	}


	@Override
	public List<User> queryUserBydeptid(UserVo userVo) {
		// TODO Auto-generated method stub
		return userMapper.queryAllUser(userVo);
	}


	@Override
	public Integer queryMaxOrderNumber() {
		// TODO Auto-generated method stub
		return userMapper.queryMaxOrderNumber();
	}


	@Override
	public Integer updateUserPwd(UserVo userVo) {
		// TODO Auto-generated method stub
		User user = userMapper.selectByPrimaryKey(userVo.getId());
		String salt = Md5Utils.createRandomStr();
		userVo.setSalt(salt);
		user.setPwd(Md5Utils.md5(SYSConstast.ADD_USER_PASSWORD, salt, 2));
		
		return userMapper.updateByPrimaryKeySelective(user);
	}


	@Override
	public User queryUserByMgr(Integer id) {
		// TODO Auto-generated method stub
		return userMapper.selectByPrimaryKey(id);
	}


	@Override
	public Integer saveUserRole(UserVo userVo) {
		// TODO Auto-generated method stub
		Integer userId=userVo.getId();
		Integer[] roleIds=userVo.getIds();
		//根据用户id删除sys_role_user里面的数据
		this.userMapper.deleleUserRoleByUserId(userId);
		//保存
		if(null!=roleIds&&roleIds.length>0) {
			for (Integer rid : roleIds) {
				this.userMapper.saveUserRole(userId,rid);
			}
		}
		return 1;
	}

}
